package com.yitiao.multithread.case02;

import lombok.SneakyThrows;

import java.util.concurrent.TimeUnit;

/**
 * <h3>线程的终止和恢复：suspend & resume</h3>
 * <p>suspend：线程暂停的时候不会释放锁</p>
 */
public class ThreadSuspendDemo {
    @SneakyThrows
    public static void main(String[] args) {
        Thread suspendThread = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                System.out.print(i + " ");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        suspendThread.start();
        TimeUnit.SECONDS.sleep(5);
        //暂停线程
        suspendThread.suspend();
        System.out.print("线程暂停 ");

        TimeUnit.SECONDS.sleep(2);
        //从之前暂停的位置恢复继续执行
        System.out.print(" 线程恢复 ");
        suspendThread.resume();
    }
}
